<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

//declare (strict_types = 1);

namespace app\home\controller;

use app\home\BaseController;
use app\admin\model\User as UserList;
//use app\admin\model\WckcStudent as WckcStudentModel;
use app\admin\model\CourseXinxikeji as CourseXinxikejiModel;
use think\facade\Db;
use think\facade\View;

class Test extends BaseController
{
    
    public function index()
    {
        $uid = get_login_user('id');
            $current_time=time();
            //dd($current_time);
            //dd($uid);
    $checkin_time=$current_time-24*60*60;
    // dump(24*60*60);
    // dump($current_time);dump($checkin_time);
    $uid=2;
    $check_time=Db::name('Checkin')->where(['uid'=>$uid])->order('create_time desc')->value('create_time');
   // dd($check_time);
    
    $return= $check_time+24*60*60<$current_time ? true : false;
    dd($return);
        $where=[];
        $param = get_params();
           $param['grade']='3A';
            $where = [];
            if (!empty($param['keywords'])) {
                $where[] = ['title|grade', 'like', '%' . $param['keywords'] . '%'];
            }
           if (!empty($param['grade'])) {
                $where[] = ['grade', '=', $param['grade']];
            }
     $this->courseModel=new CourseXinxikejiModel();

     //$list=$this->courseModel->getCourseXinxikejiList($where, $param);//->toArray()
     //$list2=$this->courseModel->getAllCourseList($where, $param);
     // dump($list);
     // dump($list2);die;

     return view();
       
    }
public function index3(){
    $username='caohongdi';
                $map=[['username','like',$username.'%']];
                dump($map);
                $count=Db::name('User')->where($map)->count();
                $list=Db::name('User')->where($map)->select();
                dump($list);
                dump($count);die;

}
   public function index2()
    {
          // if (request()->isAjax()) {
            $this->courseModel=new CourseXinxikejiModel();
            $param = get_params();
            $param['grade']=!empty($param['grade'])?$param['grade']:'4A';
            $where = [];
            if (!empty($param['keywords'])) {
                $where[] = ['title|grade', 'like', '%' . $param['keywords'] . '%'];
            }
           if (!empty($param['grade'])) {
                $where[] = ['grade', '=', $param['grade']];
            }
            $param['order']=['pid'=>'desc','sort'=>'asc'];
            $list = $this->courseModel->getCourseXinxikejiList($where,$param)->toArray();
            
             
            $data=$list['data'];
            //将数据转换为树形表格数据
              $list['data']=$this->array_to_treetable($data,0);


              dump($list);die;

         //   return table_assign(0, '', $list);
         // }else{
         //     return view();
         // }

     
       
    }
public function array_to_treetable($items, $parentId = 0,$isParent='isParent') {
    $tree = array();
    foreach ($items as $item) {
        $item[$isParent]=false;        
        if ($item['pid'] == $parentId) {
            $child = $this->array_to_treetable($items, $item['id']);
            if ($child) {
                $item[$isParent]=true;
                $item['children'] = $child;
            }            
            $tree[] = $item;
        }
    }
    return $tree;
}
public function list_to_treetable($list, $pk = 'id', $pid = 'pid', $child = 'list', $root = 0, $isParent='isParent')
{
    
// 创建Tree
    $tree = array();
    if (is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] = &$list[$key];
        }

        foreach ($list as $key => $data) {
            $data[$isParent]=false;
            //dump($data);die;
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {               

                $tree[$data[$pk]] = &$list[$key];

            } else {
                if (isset($refer[$parentId])) {
                    $parent = &$refer[$parentId];
                    $parent[$child][$data[$pk]]= &$list[$key];


                }
            }
        }
        $treedata=array();
        foreach($tree as $key=>$data){
            $data[$isParent]=false;
            if(isset($data[$child])){
                if(count($data[$child])>0){
                $data[$isParent]=true;
                $data_child=array();
                foreach($data[$child] as $key =>$child_data){
                    //dump($child_data);

                    $child_data[$isParent]=false;
                    $child_data[$child]=[];
                    if(isset($child_data[$child])){
                        if(count($child_data[$child])>0){
                           $child_data[$isParent]=true;                         
                        }
                    }
                    array_push($data_child,$child_data);
                    //dump($data_child);
                  $data[$child]= $data_child;

                }                
               }
            }else{
                $data[$child]=[];
                
            }
         array_push($treedata,$data);   
        }
    }
    //dump($tree);die;
    return $treedata;
}

    //上传文件
    public function uploader(){
         //$file[]= request()->file('file');
         // 日期前綴
         $dataPath = date('Ym');
         
         //$savename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file[0], 'md5');
          $file = request()->file('file');  //这里‘file’是你提交时的name
          $md5 = $file->md5();
    try{
        // validate(['xlsFile' => [     //goodFile是你自定义的键名，目的是为了对check里数组中的
        //     'fileSize' =>50*1024*1024,//goodFile字段值进行验证;允许文件大小
        //     'fileExt'  => array('xls','xlxs'),  //允许的文件后缀
        //     ]])->check(['xlsFile'=>$file]);//也就是对上传的$file进行验证
        $saveName = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file);//保存文件名
        //$arr = ['status' => 200, 'msg' => '成功', 'path' => app()->getRootPath().'public/storage'.$saveName];
        $arr = ['status' => 200, 'msg' => '成功', 'path' => 'public/storage/'.$saveName];
        return json($arr); //返回标准json格式
    }catch (\Exception $e) {
        return $this->exceptionHandle($e,'上传失败!' . $e->getMessage(),'json','');
    }
    }
}
